package l1j.server.server;

/**
 * 时空裂痕
 */

import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Map;
import java.util.Random;

import l1j.server.server.datatables.NpcTable;
import l1j.server.server.model.L1Location;
import l1j.server.server.model.L1Object;
import l1j.server.server.model.L1Teleport;
import l1j.server.server.model.L1World;
import l1j.server.server.model.Instance.L1NpcInstance;
import l1j.server.server.model.Instance.L1PcInstance;
import l1j.server.server.serverpackets.S_SystemMessage;

public class TimeCrackContorller implements Runnable {
	
	private static TimeCrackContorller _instance;
	
	public static int _nowTeleport = -1;
	
	public static L1Location [] _tagetTeleport = new L1Location[2]; //底比斯&&提卡尔传送坐标
	
	private final L1Location [] _timeCrack = new L1Location[9]; //亚丁大陆的时空裂痕
	
	public static int _crackId = -1;
	
	public static int [] _bossID = new int[4];	//存放BOSS obj id
	
	public static int _reopen = -1;	//底比斯和库库尔坎延长开放
	
	public static int _reopenTimer = -10;	//延长开放计时器
	
	public static TimeCrackContorller getInstance() {
		if (_instance == null) {
			_instance = new TimeCrackContorller();
		}
		return _instance;
	}
	
	private TimeCrackContorller() {
		//初始化底比斯和提卡尔传送坐标
		if (_tagetTeleport[0]==null && _tagetTeleport[1]==null) {
			L1Location loc1 = new L1Location(32624, 32898, 780);
			_tagetTeleport[0] = loc1;
			L1Location loc2 = new L1Location(32794, 32753, 783);
			_tagetTeleport[1] = loc2;
		}
		//初始化亚丁大陆传送点
		if(_timeCrack[0]==null) {
			L1Location loc1 = new L1Location(32848, 32639, 4);
			_timeCrack[0] = loc1;
			L1Location loc2 = new L1Location(32852, 32705, 4);
			_timeCrack[1] = loc2;
			L1Location loc3 = new L1Location(32728, 32709, 4);
			_timeCrack[2] = loc3;
			L1Location loc4 = new L1Location(32913, 33425, 4);
			_timeCrack[3] = loc4;
			L1Location loc5 = new L1Location(32913, 33168, 4);
			_timeCrack[4] = loc5;
			L1Location loc6 = new L1Location(32957, 33247, 4);
			_timeCrack[5] = loc6;
			L1Location loc7 = new L1Location(34255, 33203, 4);
			_timeCrack[6] = loc7;
			L1Location loc8 = new L1Location(34232, 33312, 4);
			_timeCrack[7] = loc8;
			L1Location loc9 = new L1Location(34276, 33359, 4);
			_timeCrack[8] = loc9;
		}
		//初始化BOSS ID
		_bossID[0] = -1;
		_bossID[1] = -1;
		_bossID[2] = -1;
		_bossID[3] = -1;
	}
	//当前时间
	private static GregorianCalendar _cgc;
	//检查底比斯或提卡尔开放时间
	private boolean checkDBSTime() {
		_cgc = new GregorianCalendar();
		if (_cgc.get(Calendar.HOUR_OF_DAY) >= 20 && _cgc.get(Calendar.HOUR_OF_DAY) <= 22) {
			return true;
		}
		return false;
	}
	
	public static int _bossCheckTime = -10;
	
	public static String _s = "";
	
	private void spawnCrack() {
		try {
			if (_dbsOpen) {
				return;
			}
			Random rnd = new Random();
			int rloc = rnd.nextInt(9);
			int rt = rnd.nextInt(2);
			if (rt==0) { //底比斯
				_nowTeleport = 0;
			} else if (rt==1) {	//库库尔坎
				_nowTeleport = 1;
			}
			if (rloc>=0 && rloc<=2) {
				_s = "古战场";
			} else if (rloc>=3 && rloc<=5) {
				_s = "沙漠";
			} else if (rloc>=6 && rloc<=8) {
				_s = "黄昏山脉";
			}
			L1NpcInstance npc = NpcTable.getInstance().newNpcInstance(96003);
			_crackId = IdFactory.getInstance().nextId();
			npc.setId(_crackId);
			npc.setX(_timeCrack[rloc].getX());
			npc.setY(_timeCrack[rloc].getY());
			npc.setMap((short)_timeCrack[rloc].getMapId());
			npc.setHeading(0);
			L1World.getInstance().storeObject(npc);
			L1World.getInstance().addVisibleObject(npc);
			npc.turnOnOffLight();
			
			L1World.getInstance().broadcastPacketToAll(new S_SystemMessage("[系统公告] 时空裂痕开启"));
			L1World.getInstance().broadcastPacketToAll(new S_SystemMessage("[系统公告] 裂痕 @ "+npc.getX()
					+" "+npc.getY()+" "+_s));
			_dbsOpen = true;
			if (_bossCheckTime == -10) {
				
//				Random rn = new Random();
//				_bossCheckTime = (rn.nextInt(31)+31)*60;	
//				_bossCheckTime = 9000;	//2个半小时
				_bossCheckTime = 90;
			}
		} catch (Exception e) {
			return;
		}
	}
	
	private void checkBoss() {
		//检查BOSS
		int _iend = 0;
		for (Map.Entry<Integer, L1Object> obj : L1World.getInstance().getAllVisibleObjects().entrySet()) {
			if (obj.getValue() instanceof L1NpcInstance) {
				L1NpcInstance l1npc = (L1NpcInstance) obj.getValue();
				if (l1npc!=null) {
					if (_nowTeleport==0 && l1npc.getNpcId()==46123) { //底比斯
						_iend = 1;
						_bossID[0] = l1npc.getId(); //底比斯 荷魯斯
					} else if (_nowTeleport==0 && l1npc.getNpcId()==46124) { //底比斯
						_iend = 1;
						_bossID[1] = l1npc.getId();	//底比斯 阿努比斯
					} else if (_nowTeleport==1 && l1npc.getNpcId()==95018) { //提卡尔
						_iend = 1;
						_bossID[2] = l1npc.getId(); //杰弗雷庫(雄)
					} else if (_nowTeleport==1 && l1npc.getNpcId()==95019) { //提卡尔
						_iend = 1;
						_bossID[3] = l1npc.getId();	//杰弗雷庫(雌)
					}
				}
			}
		}
		if (_iend==0) { //没有BOSS生成
			if (_nowTeleport==0) {
				spwanBoss(46123, 32782, 32836, (short)782);
				spwanBoss(46124, 32782, 32827, (short)782);
			} else if (_nowTeleport==1) {
				spwanBoss(95018, 32751, 32871, (short)784);
				spwanBoss(95019, 32750, 32859, (short)784);
			}
		}
	}
	
	private void spwanBoss(int bossid, int x, int y, short mapids) {
		L1NpcInstance npc = NpcTable.getInstance().newNpcInstance(bossid);
		int npcid = IdFactory.getInstance().nextId();
		if (bossid == 46123 && _bossID[0] == -1) {
			_bossID[0] = npcid;
		} else if (bossid == 46124 && _bossID[1] == -1) {
			_bossID[1] = npcid;
		} else if (bossid == 95018 && _bossID[2] == -1) {
			_bossID[2] = npcid;
		} else if (bossid == 95019 && _bossID[3] == -1) {
			_bossID[3] = npcid;
		}

		npc.setId(npcid);
		npc.setX(x);
		npc.setY(y);
		npc.setMap(mapids);
		if (bossid == 46123 || bossid == 46124) {
			npc.setHeading(6);
		} else if (bossid == 95018 || bossid == 95019) {
			npc.setHeading(5);
		}
		L1World.getInstance().storeObject(npc);
		L1World.getInstance().addVisibleObject(npc);
		npc.turnOnOffLight();
		npc.startChat(L1NpcInstance.CHAT_TIMING_APPEARANCE);
	}
	
	private static L1Location _goback = new L1Location(34056, 32277, 4);
	
	private void teleportAllPlayer() {
		for (L1PcInstance pc : L1World.getInstance().getAllPlayers()) {
			if (pc.getMapId()>=780 && pc.getMapId()<=784) {
				L1Teleport.teleport(pc, _goback, 0, true);
			}
		}
	}
	
	private void clear() {
		int i = 0;
		for (i=0; i<_bossID.length; i++) {
			_bossID[i]=-1;
		}
		_nowTeleport = -1;
		_crackId = -1;
		_reopenTimer=-10;
		_reopen=-1;
	}
	
	private static boolean _dbsOpen = false;
	
//	public static int _JebRequieTimer = -100;
	
//	private static int _exist = 0;
	
//	public static L1MonsterInstance _left = null;
	
//	public static L1MonsterInstance _right = null;
	
//	private static L1Object obj = null;
	
//	private static boolean checkJebRequie() {
		//判断有一个蛇头被打掉
//		_left = null;
//		_right = null;
//		if (_bossID[2]!=-1) {
//			obj = null;
//			obj = L1World.getInstance().findObject(_bossID[2]);
//			if (obj instanceof L1MonsterInstance) {
//				_left = (L1MonsterInstance) obj;
//				if (!_left.isAiRunning()) {
//					
//				}
//			}
//		}
//		
//		if((_bossID[2]!=-1 && L1World.getInstance().findObject(_bossID[2])!=null)
//				&& _bossID[3]==-1) {
//			_exist = 2;
//			_bossID[3]=-1;
//			return true;
//		} else if (_bossID[2]==-1
//				&& (_bossID[3]!=-1 && L1World.getInstance().findObject(_bossID[3])!=null)) {
//			_exist = 3;
//			_bossID[2]=-1;
//			return true;
//		}
//		return false;
//	}
	
	private static boolean checkJebRequieS(int i) {
		if ((_bossID[i]!=-1 && L1World.getInstance().findObject(_bossID[i])!=null)) {
			return true;
		}
		return false;
	}
	
	@Override
	public void run() {
		while (true) {
			if (checkDBSTime() && !_dbsOpen) {
				spawnCrack();
			}
			if (checkDBSTime() && _dbsOpen) {
				if (_bossCheckTime>0) {
					_bossCheckTime--;
				}
				if (_bossCheckTime==0) {
					checkBoss();
					_bossCheckTime=-5;
				}
			}
			if (!checkDBSTime()) {
				if (_dbsOpen) {
					if (_nowTeleport==0 && _reopen==-1) {	//底比斯
						//底比斯BOSS全部清掉
						if ((_bossID[0]!=-1 && L1World.getInstance().findObject(_bossID[0])==null)
								&& (_bossID[1]!=-1 && L1World.getInstance().findObject(_bossID[1])==null)) {
							_reopen = 1;
							_bossID[0]=-1;
							_bossID[1]=-1;
							if (_reopenTimer==-10) {
								_reopenTimer = 15*3600;
							}
							L1World.getInstance().broadcastPacketToAll(new S_SystemMessage("[系统公告] 时空裂痕延长开放中"));
						} else {
							teleportAllPlayer();
							L1World.getInstance().broadcastPacketToAll(new S_SystemMessage("[系统公告] 时空裂痕关闭"));
							_dbsOpen = false;
							L1Object obj = L1World.getInstance().findObject(_crackId);
							if (obj!=null) {
								if (obj instanceof L1NpcInstance) {
									L1NpcInstance npc = (L1NpcInstance) obj;
									npc.deleteMe();
								}
							}
							clear();
						}
					}
					if (_nowTeleport==1 && _reopen==-1) {	//库库尔坎
//						checkJebRequie();
						//库库尔坎双蛇被清掉
						if ((_bossID[2]!=-1 && L1World.getInstance().findObject(_bossID[2])==null)
								&& (_bossID[3]!=-1 && L1World.getInstance().findObject(_bossID[3])==null)) {
							_reopen = 1;
							_bossID[2]=-1;
							_bossID[3]=-1;
							if (_reopenTimer==-10) {
								_reopenTimer = 15*3600;
							}
							L1World.getInstance().broadcastPacketToAll(new S_SystemMessage("[系统公告] 时空裂痕延长开放中"));
						} else {
							teleportAllPlayer();
							L1World.getInstance().broadcastPacketToAll(new S_SystemMessage("[系统公告] 时空裂痕关闭"));
							_dbsOpen = false;
							L1Object obj = L1World.getInstance().findObject(_crackId);
							if (obj!=null) {
								if (obj instanceof L1NpcInstance) {
									L1NpcInstance npc = (L1NpcInstance) obj;
									npc.deleteMe();
								}
							}
							clear();
						}
					}
					if (_reopen==1 && _reopenTimer!=-10) {
						_reopenTimer--;
						if (_reopenTimer==0) {
							teleportAllPlayer();
							L1World.getInstance().broadcastPacketToAll(new S_SystemMessage("[系统公告] 时空裂痕关闭"));
							_dbsOpen = false;
							L1Object obj = L1World.getInstance().findObject(_crackId);
							if (obj!=null) {
								if (obj!=null) {
									if (obj instanceof L1NpcInstance) {
										L1NpcInstance npc = (L1NpcInstance) obj;
										npc.deleteMe();
									}
								}
							}
							clear();
						}
					}
				}
			}
//			if (_bossCheckTime==-5) {
//				if (checkJebRequie() && _JebRequieTimer<0) {
//					_JebRequieTimer = 60;
//				}
//				if (_JebRequieTimer>0) {
//					_JebRequieTimer--;
//				}
//				if (_JebRequieTimer==0 && _exist!=0 && checkJebRequieS(_exist)) {
//					if (_exist==2) {
//						spwanBoss(95019, 32750, 32859, (short)784);
//						_exist = 0;
//					} else if (_exist==3) {
//						spwanBoss(95018, 32751, 32871, (short)784);
//						_exist = 0;
//					}
//					_JebRequieTimer = -100;
//				}
//			}
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
}
